package com.andong.service.impl;

import com.andong.domain.entity.Student;
import com.andong.domain.pojo.PageResult;
import com.andong.domain.pojo.Result;
import com.andong.utils.AliOSSUtils;
import com.andong.utils.BaseContext;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.andong.domain.entity.SubjectVideo;
import com.andong.service.SubjectVideoService;
import com.andong.mapper.SubjectVideoMapper;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

import static com.andong.utils.SystemConstant.STUDENT;
import static com.andong.utils.SystemConstant.*;

/**
* @author 王海云
* @description 针对表【subject_video(科目二三视频表)】的数据库操作Service实现
* @createDate 2024-02-13 21:34:38
*/
@Service
public class SubjectVideoServiceImpl extends ServiceImpl<SubjectVideoMapper, SubjectVideo>
    implements SubjectVideoService{
    @Resource
    private AliOSSUtils aliOSSUtils;
    @Resource
    private RedisTemplate<String, Object> redisTemplate;
    @Resource
    private SubjectVideoMapper subjectVideoMapper;
    @Override
    public PageResult queryList(Integer page, Integer pageSize, String name, Integer type, Integer vehicleType) {
        Page<SubjectVideo> subjectVideoPage = new Page<>(page, pageSize);
        // 构造Coach条件查询器, 得到Coach分页查询基本信息结果
        subjectVideoPage = lambdaQuery()
                .like(name != null, SubjectVideo::getName, name)
                .eq(type != null, SubjectVideo::getType, type)
                .eq(vehicleType != null, SubjectVideo::getVehicleType, vehicleType)
                .page(subjectVideoPage);

        return new PageResult(subjectVideoPage.getTotal(), subjectVideoPage.getRecords());
    }

    @Override
    public Result deleteByIds(List<Long> ids) {
        // 删除阿里云视频
        List<SubjectVideo> list = lambdaQuery()
                .in(SubjectVideo::getId, ids)
                .list();
        list.forEach(subjectVideo -> {
            if (Objects.nonNull(subjectVideo.getVideo())) {
                aliOSSUtils.delete(subjectVideo.getVideo());
            }
        });
        // 删除题目
        removeByIds(ids);
        // 清空缓存
        ids.forEach(id -> redisTemplate.delete(VIDEO_CACHE + id));
        return Result.success();
    }

    @Override
    public Result saveOrModify(SubjectVideo subjectVideo) {
        if (subjectVideo.getId() != null) {
            // 修改
            // 判断是否修改了视频
            String oldVideo = lambdaQuery()
                    .eq(SubjectVideo::getId, subjectVideo.getId())
                    .one()
                    .getVideo();
            if (!oldVideo.equals(subjectVideo.getVideo())) {
                // 删除阿里云视频
                aliOSSUtils.delete(oldVideo);
            }
            updateById(subjectVideo);
            return Result.success();
        }
        // 新增
        save(subjectVideo);
        return Result.success();
    }
}




